//
//  HLLiveUserView.m
//  YueYue
//
//  Created by hulu001 on 2024/3/12.
//

#import "HLLiveUserView.h"
#import "HLLiveOptionRequest.h"
#import "HLUserInfoRequest.h"

@implementation HLLiveUserView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupViews];
    }
    return self;
}

- (void)setupViews
{
    self.layer.cornerRadius = 21;
    self.layer.masksToBounds = YES;
    self.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.5];
    
    self.avatarView = [[UIImageView alloc] init];
    _avatarView.layer.cornerRadius = 16;
    _avatarView.layer.masksToBounds = YES;
    _avatarView.userInteractionEnabled = YES;
    [self addSubview:_avatarView];
    [_avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(32);
        make.left.top.mas_equalTo(5);
        make.bottom.mas_equalTo(-5);
    }];
    
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(heartAction)];
    [_avatarView addGestureRecognizer:tapGes];
    
    self.nickLabel = [UILabel.alloc init];
    _nickLabel.textColor = UIColor.whiteColor;
    _nickLabel.font = UIFontMediumMake(12);
    _nickLabel.text = @"";
    [self addSubview:_nickLabel];
    [_nickLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_avatarView).offset(2);
        make.left.equalTo(_avatarView.mas_right).offset(5);
        make.right.mas_lessThanOrEqualTo(-15);
    }];
    
    
    self.heartButton = [[QMUIButton alloc] init];
    [_heartButton setImage:UIImageMake(@"live_heart") forState:UIControlStateNormal];
    [_heartButton setTitle:@"13234" forState:UIControlStateNormal];
    [_heartButton setTitleColor:UIColorWhite forState:UIControlStateNormal];
    [_heartButton addTarget:self action:@selector(heartAction) forControlEvents:UIControlEventTouchUpInside];
    _heartButton.titleLabel.font = UIFontMake(8);
    _heartButton.imagePosition = QMUIButtonImagePositionLeft;
    _heartButton.spacingBetweenImageAndTitle = 4;
    [self addSubview:_heartButton];
    [_heartButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nickLabel);
        make.bottom.equalTo(_avatarView).offset(-2);
        make.right.mas_lessThanOrEqualTo(-25);
    }];
    
    self.followButton = [QMUIButton buttonWithType:UIButtonTypeCustom];
    _followButton.layer.cornerRadius = 12;
    _followButton.layer.masksToBounds = YES;
    _followButton.backgroundColor = HLMainColor;
    _followButton.titleLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightMedium];
    [_followButton setTitle:@"关注" forState:UIControlStateNormal];
    [_followButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    _followButton.spacingBetweenImageAndTitle = 0;
    [self addSubview:_followButton];
    _followButton.hidden = YES;

}

- (void)loadUserInfo:(HLLiveFemaleModel *)female
{
    self.female = female;
    [_avatarView sd_setImageWithURL:[NSURL URLWithString:female.femaleAvatar] placeholderImage:[HLUIHelper avatarPlaceholder]];
    _nickLabel.text = female.femaleNickName;
    [_heartButton setTitle:female.liveLikeCount forState:UIControlStateNormal];
    [self followRequest];
    
}

- (void)heartAction
{
    [[[HLLiveOptionRequest alloc] initWithRoom:_female.liveRoomCode type:HLLiveOptionTypeThumb] startWithCompletionBlockWithSuccess:^(__kindof HLLiveOptionRequest * _Nonnull request) {
        if (request.isSuccess) {
            self.female.liveLikeCount = [NSString stringWithFormat:@"%ld", ([self.female.liveLikeCount integerValue] + 1)];
            [self.heartButton setTitle:self.female.liveLikeCount forState:UIControlStateNormal];
        }
    } failure:nil];
}



- (void)followRequest
{
    [[[HLUserInfoRequest alloc] initWithUserId:_female.femaleUserId] startWithCompletionBlockWithSuccess:^(__kindof HLUserInfoRequest * _Nonnull request) {
        
        if (request.isSuccess) {
            HLUserInfoModel *user = [request user];
            if (![user.followType isEqualToString:@"0"]) {
                self.followButton.hidden = NO;
                [self.followButton mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.size.mas_equalTo(CGSizeMake(48, 24));
                    make.right.mas_equalTo(-8);
                    make.centerY.mas_equalTo(0);
                    make.left.equalTo(self.nickLabel.mas_right).offset(10);
                }];
            } else {
                self.followButton.hidden = YES;
                [self.followButton mas_remakeConstraints:^(MASConstraintMaker *make) {
                    
                }];
            }
        }
        
    } failure:nil];
}



@end
